package reportd

import (
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
)

const (
	// DomainID 领域唯一标志
	DomainID = "report"
	// Report 标识
	Report = "report"
)

// DomainOption 域构建参数
type DomainOption struct {
	ReportRepo ReportRepo
}

// NewDomain 核心子域内容信息域
func NewDomain(p ddd.Domain, opt DomainOption) ReportDomain {
	d := &reportDomain{}
	d.Init(d, p, DomainID)
	initReport(d, opt.ReportRepo)
	return d
}

// ReportDomain 信息域
type ReportDomain interface {
	ddd.Domain
	ReportAggregateRoot() ReportAggregateRoot
}
type reportDomain struct {
	ddd.BaseDomain
	reportAggregateRoot ReportAggregateRoot
}

func (i *reportDomain) ReportAggregateRoot() ReportAggregateRoot {
	if i.reportAggregateRoot != nil {
		return i.reportAggregateRoot
	}
	r := i.BaseDomain.AggregateRootByID(Report)
	if r == nil {
		panic("没有初始化ReportAggregateRoot")
	}
	ir, ok := r.(ReportAggregateRoot)
	if !ok {
		panic("初始化ReportAggregateRoot，类型错误")
	}
	i.reportAggregateRoot = ir
	return ir

}
